<?php

class sessionController extends controller
{
	public function connexionAction()
	{
		$this->template->title = "{GDA} Connexion";
		$error = false;
		if (!isset($this->POST['login']) || !isset($this->POST['password'])){
			$this->template->setView("sidebar");
			$this->template->setView("connexion");
			return;
		}
		if (isset($this->POST['login']) && $this->POST['login'] == ""){
			$error = true;
			$this->template->error_login = "Veulliez remplir ce champ";
		}
		if (isset($this->POST['password']) && $this->POST['password'] == ""){
			$error = true;
			$this->template->error_password = "Veulliez remplir ce champ";
		}
		$res = $this->model->connexionValide($this->POST['login'], $this->POST['password']);
		if ($res['response'] == true){
			$_SESSION['login'] = $this->POST['login'];
			header("Location: /");
		}else{
			if (isset($this->POST['login']) && $this->POST['login'] != "")
				$this->template->prev_login = $this->POST['login'];
			if ($error == false){
				$this->template->error_login = "Identifiant incorrect";
				$this->template->error_password = "Mot de passe incorrect";
			}
			$this->template->setView("sidebar");
			$this->template->setView("connexion");
		}
	}

	public function inscriptionAction()
	{
		$this->template->title = "{GDA} Inscription";
		$error = false;
		if (!isset($this->POST['login']) || !isset($this->POST['password']) || !isset($this->POST['password_confirm']) || !isset($this->POST['email'])){
			$this->template->setView("sidebar");
			$this->template->setView("inscription");
			return;
		}
		if (isset($this->POST['login']) && $this->POST['login'] == ""){
			$error = true;
			$this->template->error_login = "Veulliez remplir ce champ";
		}
		if (isset($this->POST['password']) && $this->POST['password'] == ""){
			$error = true;
			$this->template->error_password = "Veulliez remplir ce champ";
		}
		if (isset($this->POST['password_confirm']) && $this->POST['password_confirm'] == ""){
			$error = true;
			$this->template->error_password_confirm = "Veulliez remplir ce champ";
		}
		if (isset($this->POST['email']) && $this->POST['email'] == ""){
			$error = true;
			$this->template->error_email = "Veulliez remplir ce champ";
		}
		$done = false;
		if (!$error){
			$res = $this->model->inscriptionValide($this->POST['login'], $this->POST['password'], $this->POST['password_confirm'], $this->POST['email']);
			if (isset($res) && isset($res['error']) && $res['error'] == true){
				if (isset($res['error_login']))
					$this->template->error_login = $res['error_login'];
				if (isset($res['error_password']))
					$this->template->error_password = $res['error_password'];
				if (isset($res['error_password_confirm']))
					$this->template->error_password_confirm = $res['error_password_confirm'];
				if (isset($res['error_email']))
					$this->template->error_email = $res['error_email'];
			}
			else {
				$done = true;
			}
		}
		if (!$done){
			if (isset($this->POST['login']) && $this->POST['login'] != "")
				$this->template->prev_login = $this->POST['login'];
			if (isset($this->POST['password']) && $this->POST['password'] != "")
				$this->template->prev_password = $this->POST['password'];
			if (isset($this->POST['password_confirm']) && $this->POST['password_confirm'] != "")
				$this->template->prev_password_confirm = $this->POST['password_confirm'];
			if (isset($this->POST['email']) && $this->POST['email'] != "")
				$this->template->prev_email = $this->POST['email'];
		}else{
			header("Location: /session/connexion");
		}
		$this->template->setView("sidebar");
		$this->template->setView("inscription");
	}

	public function informationsAction(){
		$this->template->mois = array(
			"Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre" 
		);
		$annees = array();
		for ($i = 1980; $i <=  2012; $i++)
			$annees[] = $i;
		$this->template->annees = $annees;
		$this->template->setView("sidebar");
		$this->template->setView("informations");
	}
}
?>